﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GraphicsEditor
{
    public partial class PenDialog : Form
    {
        private ShapePen pen = new ShapePen();
        public ShapePen Pen
        {
            get
            {
                return pen;
            }
            set
            {
                pen = value;
                UpdateControls();                
            }
        }

        public PenDialog(ShapePen pen)
        {
            InitializeComponent();
            InitializeComboBoxes();
            this.pen = pen;
            UpdateControls();
        }

        private void UpdateControls()
        {
            dashStyleComboBox.SelectedItem = pen.DashStyle;
            startCapComboBox.SelectedItem = pen.StartCap;
            endCapComboBox.SelectedItem = pen.EndCap;
            thicknessNumericUpDown.Value = pen.Width;
            colorButton.BackColor = Color.FromArgb(255, pen.Color.R, pen.Color.G, pen.Color.B);
            alphaNumericUpDown.Value = pen.Color.A;
        }

        private void InitializeComboBoxes()
        {
            dashStyleComboBox.DataSource = Enum.GetValues(typeof(DashStyle));
            startCapComboBox.DataSource = Enum.GetValues(typeof(LineCap));
            endCapComboBox.DataSource = Enum.GetValues(typeof(LineCap));
            viewComboBox.DataSource = Enum.GetValues(typeof(GraphicsEditor.PreviewView));
        }

        private void previewPanel_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(pen.GetPen(), new Point(GraphicsEditor.PREVIEW_MARGIN, previewPanel.Height / 2), new Point(previewPanel.Width - GraphicsEditor.PREVIEW_MARGIN, previewPanel.Height / 2));
        }

        private void viewComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            GraphicsEditor.PreviewView viewBackground = (GraphicsEditor.PreviewView)viewComboBox.SelectedValue;
            switch (viewBackground)
            {
                case GraphicsEditor.PreviewView.Canvas:
                    previewPanel.BackgroundImage = null;
                    previewPanel.BackColor = GraphicsEditor.CANVAS_COLOR;
                    break;
                case GraphicsEditor.PreviewView.Checkerboard:
                    previewPanel.BackgroundImage = Properties.Resources.checkerboard;
                    break;
            }
        }

        private void dashStyleComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            pen.DashStyle = (DashStyle)dashStyleComboBox.SelectedValue;
            previewPanel.Refresh();
        }

        private void startCapComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            pen.StartCap = (LineCap)startCapComboBox.SelectedValue;
            previewPanel.Refresh();
        }

        private void endCapComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            pen.EndCap = (LineCap)endCapComboBox.SelectedValue;
            previewPanel.Refresh();
        }

        private void thicknessNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            pen.Width = (int)thicknessNumericUpDown.Value;
            previewPanel.Refresh();
        }

        private void colorButton_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                colorButton.BackColor = colorDialog.Color;
                pen.Color = Color.FromArgb((int)alphaNumericUpDown.Value, colorButton.BackColor);
            }
            previewPanel.Refresh();
        }

        private void alphaNumericUpDown_ValueChanged(object sender, EventArgs e)
        {
            pen.Color = Color.FromArgb((int)alphaNumericUpDown.Value, colorButton.BackColor);
            previewPanel.Refresh();
        }
    }
}
